package com.hb.trade.server.project.file.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hb.core.pojo.parent.MPBaseController;
import com.hb.core.pojo.project.file.dto.ReviewItemFirstDto;
import com.hb.core.pojo.project.file.dto.ReviewItemSecondDto;
import com.hb.core.pojo.project.file.dto.ReviewItemThirdDto;
import com.hb.core.pojo.project.file.entity.ReviewItemFirst;
import com.hb.core.pojo.project.file.entity.ReviewItemSecond;
import com.hb.trade.server.project.file.service.ReviewItemFirstService;
import com.hb.trade.server.project.file.service.ReviewItemSecondService;
import com.hb.trade.server.project.file.service.ReviewItemThirdService;
import io.swagger.annotations.Api;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 评审项 前端控制器
 * </p>
 *
 * @author tian
 * @since 2022-04-13
 */
@Api(tags = "招标文件(评审项)", value = "招标文件(评审项)")
@RestController
public class ReviewItemController extends MPBaseController<ReviewItemFirst, ReviewItemFirstService> {

    protected final ReviewItemSecondService reviewItemSecondService;
    protected final ReviewItemThirdService reviewItemThirdService;

    public ReviewItemController(ReviewItemFirstService service,
                                ReviewItemSecondService reviewItemSecondService,
                                ReviewItemThirdService reviewItemThirdService) {
        super(service);
        this.reviewItemSecondService = reviewItemSecondService;
        this.reviewItemThirdService = reviewItemThirdService;
    }

    /**
     * 查询评审项详情
     *
     * @param reviewItemFirstDto
     * @return org.fzzn.core.model.msg.Message<java.util.List < com.hb.core.pojo.project.file.dto.ReviewItemFirstDto>>
     * @author tian
     * @date 2022-4-13 10:43
     */
    @PostMapping("/reviewItem/selectReviewItem")
    public Message<List<ReviewItemFirstDto>> selectReviewItem(@RequestBody ReviewItemFirstDto reviewItemFirstDto) {
        List<ReviewItemFirstDto> reviewItemFirstDtoList = service.selectReviewItem(reviewItemFirstDto);
        return MsgUtil.success(reviewItemFirstDtoList);
    }

    /**
     * 编辑一级评审项
     *
     * @param reviewItemFirstDto
     * @return org.fzzn.core.model.msg.Message<java.lang.Boolean>
     * @author tian
     * @date 2022-4-13 11:21
     */
    @PostMapping("/reviewItem/editReviewItem")
    public Message<String> editReviewItem(@RequestBody ReviewItemFirstDto reviewItemFirstDto) {
        Boolean aBoolean = service.editReviewItem(reviewItemFirstDto);
        return aBoolean ? MsgUtil.success() : MsgUtil.fail("操作失败");
    }


    /**
     * 查询二级评审项列表
     *
     * @param reviewItemSecondDto
     * @return org.fzzn.core.model.msg.Message<java.util.List < com.hb.core.pojo.project.file.dto.ReviewItemSecondDto>>
     * @author tian
     * @date 2022-4-13 11:26
     */
    @PostMapping("/reviewItem/selectReviewItemSecondList")
    public Message<List<ReviewItemSecondDto>> selectReviewItemSecondList(@RequestBody ReviewItemSecondDto reviewItemSecondDto) {
        List<ReviewItemSecondDto> reviewItemSecondDtoList = reviewItemSecondService.selectReviewItemSecondList(reviewItemSecondDto);
        return MsgUtil.success(reviewItemSecondDtoList);
    }

    /**
     * 保存，编辑二级评审项
     *
     * @param reviewItemSecondDto
     * @return org.fzzn.core.model.msg.Message<java.lang.String>
     * @author tian
     * @date 2022-4-13 11:30
     */
    @PostMapping("/reviewItem/editReviewItemSecond")
    public Message<Long> editReviewItemSecond(@RequestBody ReviewItemSecondDto reviewItemSecondDto) {
        // 判断名称是否重复
        QueryWrapper<ReviewItemSecond> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ReviewItemSecond::getSecondReviewItemName, reviewItemSecondDto.getSecondReviewItemName())
                .eq(ReviewItemSecond::getFirstReviewItemId, reviewItemSecondDto.getFirstReviewItemId());
        if(null != reviewItemSecondDto.getId()){
            queryWrapper.lambda().ne(ReviewItemSecond::getId, reviewItemSecondDto.getId());
        }

        List<ReviewItemSecond> reviewItemSecondList = this.reviewItemSecondService.list(queryWrapper);
        if(!CollectionUtil.isEmpty(reviewItemSecondList)){
            MsgUtil.fail("评审项名称不允许重复，操作失败");
        }

        Long id = reviewItemSecondService.editReviewItemSecond(reviewItemSecondDto);
        if (id != null) {
            return MsgUtil.success(id);
        } else {
            return MsgUtil.fail("保存评审项失败");
        }
    }


    /**
     * 删除二级评审项
     *
     * @param reviewItemSecondDto
     * @return org.fzzn.core.model.msg.Message<java.lang.Boolean>
     * @author tian
     * @date 2022-4-13 11:30
     */
    @PostMapping("/reviewItem/deleteReviewItemSecond")
    public Message<String> deleteReviewItemSecond(@RequestBody ReviewItemSecondDto reviewItemSecondDto) {
        Boolean aBoolean = reviewItemSecondService.deleteReviewItemSecond(reviewItemSecondDto);
        return aBoolean ? MsgUtil.success() : MsgUtil.fail("操作失败");
    }


    /**
     * 查询三级评审项列表
     *
     * @param reviewItemThirdDto
     * @return org.fzzn.core.model.msg.Message<java.util.List < com.hb.core.pojo.project.file.dto.ReviewItemThirdDto>>
     * @author tian
     * @date 2022-4-13 11:45
     */
    @PostMapping("/reviewItem/selectReviewItemThirdList")
    public Message<List<ReviewItemThirdDto>> selectReviewItemThirdList(@RequestBody ReviewItemThirdDto reviewItemThirdDto) {
        List<ReviewItemThirdDto> reviewItemThirdDtoList = reviewItemThirdService.selectReviewItemThirdList(reviewItemThirdDto);
        return MsgUtil.success(reviewItemThirdDtoList);
    }

    /**
     * 保存，编辑三级评审项
     *
     * @param reviewItemThirdDto
     * @return org.fzzn.core.model.msg.Message<java.lang.String>
     * @author tian
     * @date 2022-4-13 11:49
     */
    @PostMapping("/reviewItem/editReviewItemThird")
    public Message<ReviewItemThirdDto> editReviewItemThird(@RequestBody ReviewItemThirdDto reviewItemThirdDto) {
        String msg = reviewItemThirdService.editReviewItemThird(reviewItemThirdDto);
        if (msg.indexOf("失败") != -1) {
            return MsgUtil.fail(msg);
        }
        reviewItemThirdDto.setId(Long.valueOf(msg));
        return MsgUtil.success(reviewItemThirdDto);
    }


    /**
     * 删除三级评审项
     *
     * @param reviewItemThirdDto
     * @return org.fzzn.core.model.msg.Message<java.lang.Boolean>
     * @author tian
     * @date 2022-4-13 11:52
     */
    @PostMapping("/reviewItem/deleteReviewItemThird")
    public Message<String> deleteReviewItemThird(@RequestBody ReviewItemThirdDto reviewItemThirdDto) {
        Boolean aBoolean = reviewItemThirdService.deleteReviewItemThird(reviewItemThirdDto);
        return aBoolean ? MsgUtil.success() : MsgUtil.fail("操作失败");
    }
}
